var app = new Vue({
	el: '#app',
	// delimiters: ['[[', ']]'], //修改vue读取变量的语法 {{}}改为[[]]
	data: {
		host: host,
		//v-model
		mobile: '',
		password: '',
		is_show_waiting: '',

		image_code: '',
		image_code_url: '',
		uuid: '',
		sms_code: '',
		access_token: '',
		send_info: false, //类比上厕所 send_info是锁，false为开门

		//v-show
		error_phone: false,
		error_password: false,

		error_image_code: false,
		error_sms_code: false,
		// error_allow: false,


		//错误信息
		//error_name_message
		error_mobile_messgae: '请输入手机号',
		error_image_code_message: '',
		sms_code_tip: '获取短信验证码',
		error_sms_code_message: '',



	},
	mounted() { //页面加载完调用
		this.cc();
		this.generate_image_code();


	},
	methods: { //定义实现事件方法
		generate_image_code: function() {
			let that = this
			that.uuid = generateUUID();
			axios.get(that.host + '/imgecode/' + that.uuid)
				.then(function(resp) {
					// console.log(resp.data);
					console.log('sss');
					that.image_code_url = that.host + '/imgecode/' + that.uuid + '/';
					console.log('vvv');
				})


		},
		cc: function(resp) {
			//从路径获取code
			var that = this
			var code = get_query_string('code');
			// console.log(this.host + '/oauth_callback/?code='+code);
			axios.get(this.host + '/oauth_callback/?code=' + code, {
					responseType: 'json',
					withCredentials: true,
				})
				.then(response => {
					if (response.data.code == 0) {
						// 用户已绑定
						// var state = this.get_query_string('state');
						location.href = 'http://www.meiduo.site:8080/';
					} else {
						// 用户未绑定:
						this.access_token = response.data.access_token;
						this.is_show_waiting = false;
					}
				})
				.catch(errmsg => {
					console.log(errmsg.response);
					alert('服务器异常');
				})

		},

		//校验手机号
		check_phone: function() {
			let that = this
			var re = /^1[3-9]\d{9}$/;
			if (re.test(that.mobile)) {
				axios.get(that.host + '/mobiles/' + that.mobile + '/count')
					.then(function(resp) {
						if (resp.data.count > 0) {
							console.log(resp.data.count);
							that.error_phone = false;
							that.error_mobile_messgae = '手机号存在';
						} else {
							that.error_phone = false;
						}
					})

			} else {
				that.error_mobile_messgae = '您输入的手机号码格式不正确';
				that.error_phone = true;
			}
		},
		//校验密码
		check_pwd: function() {
			var re = /^[0-9A-Za-z]{8,20}$/;
			if (re.test(this.password)) {
				this.error_password = false;
			} else {
				this.error_password = true;
			}

		},
		//校验图片验证码
		check_image_code: function() {
			if (this.image_code.length != 4) {
				this.error_image_code_message = '请输入正确的验证码'
				this.error_image_code = true;
			} else {
				this.error_image_code = false;
			}
		},
		//校验短信验证码
		check_sms_code: function() {
			if (this.sms_code.length != 6) {
				this.error_sms_code_message = '请输入短信驗證碼'
				this.error_sms_code = true;
			} else {
				this.error_sms_code = false;
			}
		},
		//发送短信验证码
		send_sms_code: function() {
			let that = this
			if (that.send_info == true) { //判断是否门为关
				return; //如果关，返回
			}
			that.send_info = true; //第一次进来，关门

			that.check_phone();
			that.check_image_code();
			if (that.error_phone == true || that.error_image_code == true) {
				that.send_info = false;
				return;
			}

			axios.get(that.host + '/smscode/' + that.mobile + '/?uuid=' + that.uuid +
					'&image_code=' + that.image_code, {
						responseType: 'json',
					})
				.then(function(resp) {
					if (resp.data.code == '0') {
						let num = 60
						//请求成功	倒计时\
						//回调函数:每隔多长时间执行一次
						// setInterval('回调函数','时间间隔')
						var t = setInterval(function() {
							if (num == 1) { //num等于1停止倒计时
								clearInterval(t);
								that.sms_code_tip = '获取短信验证码';
								//重新生成验证码
								that.generate_image_code();
								that.send_info = false; //上完出去，开门
							} else {
								num -= 1 //每秒减一
								that.sms_code_tip = num + '秒';
							}

						}, 1000) //1秒 1000毫秒

					} else {
						if (resp.data.code == '400') { //错误信息
							that.error_image_code_message = resp.data.errmsg;
							that.error_image_code = true;

						}
						that.send_info = false; //出不来开门
					}
				})
				.catch(function(errmsg) {
					console.log(errmsg.response);
					// this.error_sms_code = true;
					that.send_info = false;
				})

		},

		//校验勾选协议
		//监听表单提交
		on_submit: function() {
			let that = this;
			that.check_pwd();
			that.check_phone();
			that.check_image_code();
			that.check_sms_code();
			// this.send_sms_code();

			if (that.error_password == true || that.error_phone == true || that.error_sms_code == true ||
				that.error_image_code == true) {
				//如果以上有一个错误提示则禁用表单提交按钮
				window.event.returnValue = false;
			} else {


				var url = that.host + '/oauth_callback/'

				axios.post(url, {

						password: that.password,
						mobile: that.mobile,
						access_token: that.access_token,
						sms_code: that.sms_code,

					}, {
						responseType: 'json',
						//表示跨域请求是否需要使用凭证传递cookie信息
						withCredentials: true,
					})
					.then(response => {
						if (response.data.code == 0) {
							window.location.href = 'index.html';
						} else {
							if (response.data.code == 400) {
								that.error_sms_code_message = response.data.errmsg;
								that.error_sms_code = true
							}
						}


					})
					.catch(error => {
						alert('内部错误，联系管理员')
					})




			}
		}
	},

})
